package day10;

import java.util.Random;

//增强型字符串
public class Demo2 {
    public static void main(String[] args) {
        //1.必须先创建对象
     StringBuilder sb=new StringBuilder("java");
     StringBuffer sb2=new StringBuffer("java");

     //2.对象调用方法来操作字符串（适合做大量字符串的修改）
        sb.append("script");
        System.out.println(sb);

        //面试题：
        String str="java";
        StringBuilder sb3=new StringBuilder("java");
        System.out.println(str==sb3.toString());
        System.out.println(str==new String("java"));
        System.out.println(str.equals(sb3.toString()));

        //删除指定区间字符串内容，参数2是不包含的
        sb.delete(1,sb.length()-1);
        System.out.println(sb);
        //在指定的位置插入数据
        sb.insert(1,"scrip");
        System.out.println(sb);
        //删除指定下标的字符
        sb.deleteCharAt(sb.length()-1);
        System.out.println(sb);
        //替换指定区间内容
        sb.replace(1,sb.length(),"ava");
        System.out.println(sb);
        //将字符串反转
        sb.reverse();
        System.out.println(sb);
        //通过StringBuffer或者StringBuilder随机生成一个长度为6的字符串
        //内容 # $ % &
        StringBuilder sb4=new StringBuilder();
        Random rd=new Random();
        for (int i=0;i<6;i++){
            int n= rd.nextInt(4)+1;
            switch (n){
                case 1: sb4.append("#");break;
                case 2: sb4.append("$");break;
                case 3: sb4.append("%");break;
                case 4: sb4.append("&");break;
            }
        }
        System.out.println(sb4);
    }
}
